package com.leetcode.algorithm.y22.m09.w2;

import java.util.Arrays;

/**
 * 2160. 拆分数位后四位数字的最小和
 * 
 * https://leetcode.cn/problems/minimum-sum-of-four-digit-number-after-splitting-digits/
 * 
 * @author jie.deng
 *
 */
class Question2160Solution01 {

	public int minimumSum(int num) {
		// 各位数字排序
		char[] chars = ("" + num).toCharArray();
		Arrays.sort(chars);

		int len = chars.length;
		// 忽略前面的0
		int idx = 0;
		while (idx < len && chars[idx] == '0') {
			idx++;
		}
		
		// 剩余数位个数判断奇偶，做出相应的处理
		int num1 = 0;
		int num2 = 0;
		if ((len - idx & 1) == 1) {
			num1 *= 10;
			num1 += (chars[idx] - '0');
			idx++;
		}
		while (idx < len) {
			num1 *= 10;
			num1 += (chars[idx] - '0');
			idx++;
			num2 *= 10;
			num2 += (chars[idx] - '0');
			idx++;
		}
		return num1 + num2;
	}

}